{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "End-to-End-TFDS-pipeline.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "IPr0Bwz6y-NO"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "import tensorflow_datasets as tfds"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "model = tf.keras.models.Sequential([ \n",
        "  tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(300, 300, 3)), \n",
        "  tf.keras.layers.MaxPooling2D(2, 2),\n",
        "  tf.keras.layers.Conv2D(32, (3,3), activation='relu'), \n",
        "  tf.keras.layers.MaxPooling2D(2,2), \n",
        "  tf.keras.layers.Conv2D(64, (3,3), activation='relu'), \n",
        "  tf.keras.layers.MaxPooling2D(2,2), \n",
        "  tf.keras.layers.Flatten(), \n",
        "  tf.keras.layers.Dense(256, activation='relu'), \n",
        "  tf.keras.layers.Dense(1, activation='sigmoid')\n",
        "])"
      ],
      "metadata": {
        "id": "aR2Ib0y72hQW"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "model.compile(optimizer='Adam', loss='binary_crossentropy',metrics=['accuracy'])"
      ],
      "metadata": {
        "id": "jWC8MuFs2-n0"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "data = tfds.load('horses_or_humans', split='train', as_supervised=True) \n",
        "val_data = tfds.load('horses_or_humans', split='test', as_supervised=True)"
      ],
      "metadata": {
        "id": "HxVSv4Pu3E49"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def normalize_img(image, label):\n",
        "  \"\"\"Normalizes images: `uint8` -> `float32`.\"\"\"\n",
        "  return tf.cast(image, tf.float32) / 255., label"
      ],
      "metadata": {
        "id": "yQCT64xQ8Ev3"
      },
      "execution_count": 5,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def augment_img(image, label):\n",
        "  image, label = normalize_img(image, label)\n",
        "  image = tf.image.random_flip_left_right(image)\n",
        "  return image, label"
      ],
      "metadata": {
        "id": "ehAjPjxa3Jl8"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "data = data.cache()\n",
        "data = data.map(augment_img, num_parallel_calls=tf.data.AUTOTUNE)\n",
        "train_data = data.shuffle(1024).batch(32)\n",
        "train_data = train_data.prefetch(tf.data.AUTOTUNE)\n",
        "val_data = val_data.map(normalize_img, num_parallel_calls=tf.data.AUTOTUNE)\n",
        "val_data = val_data.batch(32)\n",
        "val_data = val_data.cache()\n",
        "val_data = val_data.prefetch(tf.data.AUTOTUNE)"
      ],
      "metadata": {
        "id": "Xp9pEtvJ3zls"
      },
      "execution_count": 7,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "% time history = model.fit(train_data, epochs=10, validation_data=val_data, validation_steps=1)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "0XJTF-G13yDK",
        "outputId": "50739df1-5c73-4f0f-97ca-cef39f7505d4"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Epoch 1/10\n",
            "33/33 [==============================] - 17s 93ms/step - loss: 0.8459 - accuracy: 0.7400 - val_loss: 0.3556 - val_accuracy: 0.8750\n",
            "Epoch 2/10\n",
            "33/33 [==============================] - 4s 71ms/step - loss: 0.1272 - accuracy: 0.9464 - val_loss: 0.9759 - val_accuracy: 0.8125\n",
            "Epoch 3/10\n",
            "33/33 [==============================] - 2s 48ms/step - loss: 0.0214 - accuracy: 0.9942 - val_loss: 1.5251 - val_accuracy: 0.8125\n",
            "Epoch 4/10\n",
            "33/33 [==============================] - 2s 47ms/step - loss: 0.0166 - accuracy: 0.9961 - val_loss: 1.7294 - val_accuracy: 0.7188\n",
            "Epoch 5/10\n",
            "33/33 [==============================] - 2s 48ms/step - loss: 0.0185 - accuracy: 0.9932 - val_loss: 0.5618 - val_accuracy: 0.8438\n",
            "Epoch 6/10\n",
            "33/33 [==============================] - 2s 48ms/step - loss: 0.0045 - accuracy: 1.0000 - val_loss: 1.0553 - val_accuracy: 0.8438\n",
            "Epoch 7/10\n",
            "33/33 [==============================] - 2s 48ms/step - loss: 0.0063 - accuracy: 0.9981 - val_loss: 1.0521 - val_accuracy: 0.8125\n",
            "Epoch 8/10\n",
            "33/33 [==============================] - 2s 48ms/step - loss: 0.0023 - accuracy: 1.0000 - val_loss: 0.6695 - val_accuracy: 0.8750\n",
            "Epoch 9/10\n",
            "33/33 [==============================] - 2s 48ms/step - loss: 5.7260e-04 - accuracy: 1.0000 - val_loss: 1.0087 - val_accuracy: 0.8750\n",
            "Epoch 10/10\n",
            "33/33 [==============================] - 2s 48ms/step - loss: 2.1146e-04 - accuracy: 1.0000 - val_loss: 1.0801 - val_accuracy: 0.8750\n",
            "CPU times: user 38.7 s, sys: 3.03 s, total: 41.7 s\n",
            "Wall time: 41 s\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# CPU times: user 37.6 s, sys: 2.73 s, total: 40.3 s\n",
        "# Wall time: 31.8 s"
      ],
      "metadata": {
        "id": "ANqVpV7H5w33"
      },
      "execution_count": 9,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# CPU times: user 37.5 s, sys: 2.79 s, total: 40.3 s\n",
        "# Wall time: 43 s"
      ],
      "metadata": {
        "id": "m27RX-Y0-bgO"
      },
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# CPU times: user 1min 41s, sys: 4.19 s, total: 1min 45s\n",
        "# Wall time: 1min 41s"
      ],
      "metadata": {
        "id": "vMsItbHz_Cnx"
      },
      "execution_count": 11,
      "outputs": []
    }
  ]
}